package com.example.demo040801.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.demo040801.R;
import com.example.demo040801.entity.BingImage;
import com.example.demo040801.view.ImageActivity;

import java.util.List;

public class BingImageAdapter extends RecyclerView.Adapter<BingImageAdapter.ViewHolder> {
    private List<BingImage> bingImages;

    public void setBingImages(List<BingImage> bingImages) {
        this.bingImages = bingImages;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bing_image, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BingImage bingImage = bingImages.get(position);
        holder.titleTextView.setText(bingImage.getTitle());

        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(v.getContext(), ImageActivity.class);
            intent.putExtra("image_url", bingImage.getUrl());
            v.getContext().startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return bingImages != null ? bingImages.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView titleTextView;

        ViewHolder(View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.titleTextView);
        }
    }
}